package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.ActivityModule;
import cn.com.connext.scrm.domain.master.ActivityModuleConfig;
import cn.com.connext.scrm.domain.master.ActivityModuleConfigChild;
import cn.com.connext.scrm.domain.master.WriteOffCode;
import cn.com.connext.scrm.service.ActivityModuleConfigService;
import cn.com.connext.scrm.service.WriteOffCodeService;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.*;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 第三方调用接口
 */
@RestController
@RequestMapping("/api/activitymoduleconfig/")
public class ActivityModuleConfigResource extends BaseResource {
    private final Logger log = LoggerFactory.getLogger(ActivityModuleConfigResource.class);
    @Autowired
    private ActivityModuleConfigService amcService;


    public ActivityModuleConfigResource() {
        super("ActivityModuleConfig");
    }

    /**
     * 新增活动组件
     */
    @ApiOperation(value = "新增活动组件", notes = "新增活动组件")
    @PostMapping(value = "/add")
    public JsonResult add(@RequestBody ActivityModuleConfigAddVM vm ){
        return  amcService.save(vm);
    }


    /**
     * 编辑组件
     */
    @ApiOperation(value = "编辑组件", notes = "编辑组件")
    @PostMapping(value = "/editCoupon")
    public JsonResult editCoupon(@RequestBody ActivityModuleConfigEditVM vm){
        JsonResult result = new JsonResult();
        try{
            result = amcService.editCoupon(vm);
        } catch (Exception e) {
            log.error(e.getMessage());
            result.setState(-1);
            result.setMessage("系统异常,查询失败");
        }
        return result;
    }

    /**
     * 查询组件
     */
    @ApiOperation(value = "查询组件", notes = "查询组件")
    @PostMapping(value = "/findById")
    public JsonResult findById(@RequestBody ActivityModuleConfig vm){
        JsonResult result = new JsonResult();
        try{
            result = amcService.findById(vm.getId());
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("系统异常,查询失败");
        }
        return result;
    }

    /**
     * 新增活动组件
     */
    @ApiOperation(value = "删除活动组件", notes = "删除活动组件")
    @PostMapping(value = "/deleteComponent")
    public JsonResult deleteComponent(@RequestBody ActivityModuleConfigVM vm ){
        return  amcService.deleteComponent(vm);
    }


    /**
     * 查询所有组件
     */
    @ApiOperation(value = "查询所有组件", notes = "查询所有组件")
    @PostMapping(value = "/findAllModule")
    public JsonResult findAllModule(){
        JsonResult result = new JsonResult();
        try{
            result = amcService.findAllModule();
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }


    /**
     * 新增活动配置模块
     */
    @ApiOperation(value = "新增活动配置模块", notes = "新增活动配置模块")
    @PostMapping(value = "/addActivityModule")
    public JsonResult addActivityModule(@RequestBody ActivityModule entity ){
        return  amcService.addActivityModule(entity);
    }

    /**
     * 新增活动配置模块
     */
    @ApiOperation(value = "新增活动配置模块list", notes = "新增活动配置模块list")
    @PostMapping(value = "/addActivityModuleList")
    public JsonResult addActivityModuleList(@RequestBody ActivityModuleVM vm ){
        return  amcService.addActivityModuleList(vm);
    }

    /**
     * 查询当前活动配置的模块
     */
    @ApiOperation(value = "查询该活动已配置的组件", notes = "查询该活动已配置的组件")
    @PostMapping(value = "/findModuleByActivityId")
    public JsonResult findModuleByActivityId(@RequestBody ActivityModule entity){
        JsonResult result = new JsonResult();
        try{
            result = amcService.findModuleByActivityId(entity.getActivityId());
        } catch (Exception e) {
            log.info("异常信息: {}",e.getMessage());
            result.setState(-1);
            result.setMessage("查询失败");
        }
        return result;
    }


    /**
     * 删除该活动已配置的组件
     */
    @ApiOperation(value = "删除该活动已配置的组件", notes = "删除该活动已配置的组件")
    @PostMapping(value = "/delete")
    public JsonResult delete(@RequestBody ActivityModule entity){
        JsonResult result = new JsonResult();
        try{
            result = amcService.delete(entity);
        } catch (Exception e) {
            result.setState(-1);
            result.setMessage("系统异常,删除失败");
        }
        return result;
    }

    /**
     * 210809
     * 模糊查询组件返回图片及组件名称
     */
    @ApiOperation(value = "模糊查询所有组件", notes = "模糊查询所有组件")
    @PostMapping(value = "/likeAllModule")
    public ResponseEntity likeAllModule(@RequestBody ActivityModuleConfigVM vm){
        JsonResult result = new JsonResult();
        try {
            result.setState(0);
            result.setMessage("根据组件名称模糊查询成功");
            result.setData(amcService.findByLikecomponentName(vm.getComponentName()));
            return entityResponse(result);
        } catch (Exception e) {
            log.error("根据组件名称模糊查询失败:{}", e);
            result.setState(1);
            result.setMessage("根据组件名称模糊查询失败");
            return entityResponse(result);
        }
    }

}

